package com.tongji.showroom.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	/**
	 * 
	 * @author zhuweichan
	 * 
	 */	
	public class Matting 
	{
		private var bitmap:Bitmap;
		
		public function Matting()
		{			
		}

		/**
		 * 抠图的具体算法
		 * @param bitmap
		 * @return Bitmap
		 * 
		 */		
		public static function mattingStart(bitmap:Bitmap):BitmapData
		{
			
			var btmpdt_source:BitmapData = bitmap.bitmapData;
			var btmpdt_new:BitmapData = new BitmapData(btmpdt_source.width, btmpdt_source.height, true, 0x00000000);
			var btm:Bitmap = new Bitmap(btmpdt_new);
			btm.smoothing = true;
			var y:uint;
			var x:uint;
			var count:int = 0;
			var step:int = 4;
			var scope:int = 15;
			
			var clr:Array = new Array();
			
			//边界的RGB值(其实是靠左侧边界的RGB平均值)
			var bound_red:int = 0;
			var bound_green:int = 0;
			var bound_blue:int = 0;
			var n:int = 0;
			
			var sum_r:int = 0;
			var sum_g:int = 0;
			var sum_b:int = 0;
			
			for (var i:int = 0; i < btmpdt_source.width; i++) 
			{
				clr.push(btmpdt_source.getPixel(i, 0));
				clr.push(btmpdt_source.getPixel(i, btmpdt_source.height - 1));
				btmpdt_new.setPixel32(i, 0, 0x00000000);
				btmpdt_new.setPixel32(i, btmpdt_source.height - 1, 0x00000000);
			}
			
			for (var j:int = 1; j < btmpdt_source.height - 1; j++) 
			{
				clr.push(btmpdt_source.getPixel(0, j));
				clr.push(btmpdt_source.getPixel(btmpdt_source.width - 1, j));
				btmpdt_new.setPixel32(0, j, 0x00000000);
				btmpdt_new.setPixel32(btmpdt_source.width - 1, j, 0x00000000);
			}
			
			for (var k:int = 0; k < clr.length; k++) 
			{
				sum_r = sum_r + (clr[k] >> 16 & 0xFF);
				sum_g = sum_g + (clr[k] >> 8 & 0xFF);
				sum_b = sum_b + (clr[k] & 0xFF);
			}
			
			var src_red:uint = sum_r / clr.length;
			var src_green:uint = sum_g / clr.length;
			var src_blue:uint = sum_b / clr.length;
			
			for(y = 1; y < btmpdt_source.height - 1; y++)
			{
				count = 0;
				
				for(x = 1; x < btmpdt_source.width - 1; x++)
				{
					var color:uint =  btmpdt_source.getPixel(x, y);
					var red:uint = color >> 16 & 0xFF;
					var green:uint = color >> 8 & 0xFF;
					var blue:uint = color & 0xFF;
					
					if((red - src_red < scope && red - src_red > -scope) && (green - src_green < scope && green - src_green > -scope) && (blue - src_blue < scope && blue - src_blue > -scope))
					{
						if(count != 0)
						{
							count--;
							var col:uint =  btmpdt_source.getPixel(x - step, y);
							btmpdt_new.setPixel32(x - step, y, col + ((0xFF * (count / step)) << 24));
						}
						btmpdt_new.setPixel32(x, y, color + (0x00 << 24));
					}
					else
					{
						if(count != step)
						{
							count++;
							btmpdt_new.setPixel32(x, y, color + ((0xFF * (count / step)) << 24));
						}
						else
						{
							n++;
							bound_red = bound_red * n / (n + 1) + red / (n + 1);
							bound_green = bound_green * n / (n + 1) + green / (n + 1);
							bound_blue = bound_blue * n / (n + 1) + blue / (n + 1);
							btmpdt_new.setPixel32(x, y, color + (0xFF << 24));
						}
					}					
				}				
			}
			
			btm.scaleX = 0.5;
			btm.scaleY = 0.5;
			return btmpdt_new;			
		}
	}
}